<?php

namespace app\admin\controller;

use think\Db;
use think\facade\Cache;

class Index extends Common
{
   /**
    * 后台首页
    */
   public function index()
   {
      return view('/index');
   }

   /***
    * 后台首页主要内容
    */
   public function main()
   {
      return view('/main');
   }

   /**
    *  客资统计
    */
   public function customer_count()
   {
      //查出网销人员
      $admin_id = Db::table('role_admin')->where('role_id', '5')->column('admin_id');
      $names = Db::table('admins')->whereIn('id', $admin_id)->column('name');
      //统计每个网销的数据
      foreach ($names as $name) {
         $datas[] = Db::table('customers')->where('wang', $name)->count();
      }

      return json(compact('names', 'datas'));
   }

   /***
    * 成交率统计
    */
   public function customer_total()
   {
      //查出网销人员
      $admin_id = Db::table('role_admin')->where('role_id', '5')->column('admin_id');
      $names = Db::table('admins')->whereIn('id', $admin_id)->column('name');

      //成交率
      foreach ($names as $name) {
         $datas[] = [
            'value' => Db::table('orders')
               ->alias('a')
               ->join('customers b', 'a.customer_id = b.id')
               ->where('b.wang', $name)
               ->where('is_order', 1)
               ->count(),
            'name' => $name
         ];
      }

      return json(compact('names', 'datas'));
   }


   /***
    * 清楚缓存
    */
   public function clear()
   {
      Cache::clear();
      return redirect('Index/index')->with('alert', '清除缓存成功');
   }

}




